define(function(require, exports, module) {

	require("loading");
	var layer = require("layer");
	var router = require("router");
	var $ = jQuery;
	var windows = [];

	$.extend({

		closeAllWindow:function() {
			var temp = [];
			for(var i=0; i<windows.length; i++) {
				temp.unshift(windows[i]);
			}
			for(var i=0; i<temp.length; i++) {
				temp[i].close();
			}
		},
		window: function(opts, params) {
			var result = {
				id: null,
				layero:null,
				show:function() {
					if(this.layero) {
						this.layero.triggerHandler("mousedown");
					}
				},
				close:function() {
					if(this.id) {
						layer.close(this.id);
					} else {
						var me = this;
						setTimeout(function() {
							me.close();
						}, 200);
					}
				}
			};
			var winRouter = null;
			var options = {};
			var defaultOptions = {
				type: typeof(opts.type) == "number" ? opts.type : 1,
				area: opts.area ? opts.area : ['820px', '500px'],
				shade: opts.shade ? opts.shade : 0,
				max: opts.max == false ? false : true,
				content: opts.content ? opts.content : '<div class="border"><div class="content"></div></div>',
			    zIndex: layer.zIndex,
			    success: function(layero) {
			    	var _this = this;
			    	result.layero = layero;
			        layer.setTop(layero);
			        var content = $(layero).find("div.content");
			        var loading = $(layero).find("div.border").loading(1);
			        try {
				        winRouter = router.route(content, {
				        	router:opts.router,
				        	beforeInit:function(vue) {
				        		vue.loading = function(op) {
				        			if(op == "close") {
				        				$(this.$el).parent("div.border").loading("close");
				        				return;
				        			}
				        			return $(layero).find("div.border").loading(1);
				        		}
				        		
				        		vue.getWindow = function() {
				        			return result;
				        		}

				        		vue.showWindow = function() {
				        			result.show();
				        		}

				        		vue.closeWindow = function() {
				        			result.close();
				        		}
				        	},
				        	success: function(vue, isInitComplete) {
				        		var title = $(vue.$el).find("title");
				        		var titleHtml = title.html();
				        		if(titleHtml) {
				        			$(layero).find(".layui-layer-title").html(titleHtml);
				        		}
				        		title.remove();
				        		if(typeof(opts.success) == "function") {
						        	opts.success.call(_this, layero);
						        }
						        if(isInitComplete) {
						        	loading.close();
						        }
				        	},
				        	error: function() {
				        		if(typeof(opts.error) == "function") {
				        			opts.error.call(_this);
				        		}
				        		result.close();
				        	}
				        }, params);
				    } catch(e) {
			    		console.log(e);
			    		result.close();
				    }
			    },
			    end: function() {
			    	if(typeof(opts.end) == "function") {
			    		opts.end.call(this);
			    	}
			    	if(winRouter) {
			    		winRouter.destroy();
			    		winRouter = null;
			    	}
			    	var index = windows.indexOf(result);
			    	if(index > -1) {
			    		windows.splice(index, 1);
			    	}
			    	result.id = null;
			    	result.layero = null;
			    }
			};
			$.extend(options, opts);
			$.extend(options, defaultOptions);
			result.id = layer.open(options);
			windows.push(result);
			return result;
		}

	});
	
	module.exports = {
		create:function(opts, params) {
			return $.window(opts, params);
		}
	};

});